﻿@* Views/AssetStatistics/Index.cshtml *@
@model List<AssetStatisticsViewModel>

@{
    ViewBag.Title = "固定资产统计";
}

<div class="container">
    <h2>固定资产统计</h2>

    @if (ViewBag.Error != null)
    {
        <div class="alert alert-danger">
            <strong>错误:</strong> @ViewBag.Error
        </div>
    }

    @if (ViewBag.Message != null)
    {
        <div class="alert alert-warning">
            @ViewBag.Message
        </div>
    }

    @if (ViewBag.SuccessMessage != null)
    {
        <div class="alert alert-success">
            @ViewBag.SuccessMessage
        </div>
    }

    @if (Model != null && Model.Any())
    {
        // 获取所有区域名称（从所有行的AreaValues中收集）
        var allAreaNames = new HashSet<string>();
        foreach (var asset in Model)
        {
            foreach (var areaName in asset.AreaValues.Keys)
            {
                allAreaNames.Add(areaName);
            }
        }
        var areaNames = allAreaNames.ToList();

        <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th>资产名称</th>
                    @foreach (var areaName in areaNames)
                    {
                        <th>@areaName</th>
                    }
                    <th>总数量</th> <!-- 总数量放在最后一列 -->
                </tr>
            </thead>
            <tbody>
                @foreach (var asset in Model)
                {
                    <tr>
                        <td>@(asset.AssetName ?? "空")</td>

                        @foreach (var areaName in areaNames)
                        {
                            <td style="text-align: center;">
                                @if (asset.AreaValues.ContainsKey(areaName) && asset.AreaValues[areaName].HasValue)
                                {
                                    @asset.AreaValues[areaName].Value.ToString("N0")
                                }
                                else
                                {
                                    <text>0</text>
                                }
                            </td>
                        }

                        <td style="text-align: center; font-weight: bold;">
                            @(asset.SNum ?? "0")
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    }
    else if (ViewBag.SuccessMessage == null && ViewBag.Error == null)
    {
        <div class="alert alert-info">暂无数据</div>
    }
</div>

<style>
    .table {
        width: 100%;
        margin-top: 20px;
    }

        .table th {
            background-color: #f8f9fa;
            text-align: center;
            font-weight: bold;
            padding: 10px;
        }

        .table td {
            padding: 8px;
            text-align: center;
        }

    .table-striped tbody tr:nth-of-type(odd) {
        background-color: rgba(0,0,0,.02);
    }

    .table-bordered {
        border: 1px solid #dee2e6;
    }

        .table-bordered th,
        .table-bordered td {
            border: 1px solid #dee2e6;
        }
</style>